﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Smled.States
{
    public partial class SelectStateToolbar : ToolStrip
    {
        private int cbW = 0, cbH = 0;
        private Tile[,] clip = null;

        public SelectStateToolbar()
        {
            InitializeComponent();
            btnErase.Click += new EventHandler(btnErase_Click);
            btnFill.Click += new EventHandler(btnFill_Click);
            btnCopy.Click += new EventHandler(btnCopy_Click);
            btnPaste.Click += new EventHandler(btnPaste_Click);
            btnBrushFill.Click += new EventHandler(btnBrushFill_Click);
        }

        void btnBrushFill_Click(object sender, EventArgs e)
        {
            if (MainForm.Instance.MainView.RectSelection && SelectedBrush != null)
            {
                RandomBrush rb = SelectedBrush;
                MapView mv = MainForm.Instance.MainView;

                for (int y = mv.SelectionUL.Y; y <= mv.SelectionDR.Y; ++y)
                    for (int x = mv.SelectionUL.X; x <= mv.SelectionDR.X; ++x)
                        mv.Map.SetTileAt(x, y, rb.GetTile());
                mv.Invalidate();
            }
        }

        void btnPaste_Click(object sender, EventArgs e)
        {
            if (clip != null)
            {
                MapView mv = MainForm.Instance.MainView;
                int bX = mv.SelectionUL.X;
                int bY = mv.SelectionUL.Y;

                for(int y = 0; y < cbH; ++y)
                    for(int x = 0; x < cbW; ++x)
                        mv.Map.SetTileAt(bX + x, bY + y, clip[x, y]);

                mv.Invalidate();
            }
        }

        void btnCopy_Click(object sender, EventArgs e)
        {
            MapView mv = MainForm.Instance.MainView;
            cbW = mv.SelectionDR.X - mv.SelectionUL.X + 1;
            cbH = mv.SelectionDR.Y - mv.SelectionUL.Y + 1;

            if (cbW > 0 && cbH > 0)
            {
                clip = new Tile[cbW, cbH];
                for (int y = mv.SelectionUL.Y, Y = 0; y <= mv.SelectionDR.Y; ++y, ++Y)
                    for (int x = mv.SelectionUL.X, X = 0; x <= mv.SelectionDR.X; ++x, ++X)
                        clip[X, Y] = mv.Map.GetTileAt(x, y);
            }
        }

        void btnFill_Click(object sender, EventArgs e)
        {
            if (MainForm.Instance.MainView.RectSelection)
            {
                MapView mv = MainForm.Instance.MainView;
                for (int y = mv.SelectionUL.Y; y <= mv.SelectionDR.Y; ++y)
                    for (int x = mv.SelectionUL.X; x <= mv.SelectionDR.X; ++x)
                        mv.Map.SetTileAt(x, y, mv.Chooser.SelectedTile);
                mv.Invalidate();
            }
        }

        void btnErase_Click(object sender, EventArgs e)
        {
            if (MainForm.Instance.MainView.RectSelection)
            {
                MapView mv = MainForm.Instance.MainView;
                for (int y = mv.SelectionUL.Y; y <= mv.SelectionDR.Y; ++y)
                    for (int x = mv.SelectionUL.X; x <= mv.SelectionDR.X; ++x)
                        mv.Map.SetTileAt(x, y, null);
                mv.Invalidate();
            }
        }

        public void RefreshList()
        {
            cbBrushes.Items.Clear();
            foreach (RandomBrush brush in RandomBrushStorage.Brushes)
                cbBrushes.Items.Add(brush);
        }

        public RandomBrush SelectedBrush
        {
            get
            {
                if (cbBrushes.SelectedItem is RandomBrush)
                {
                    return (RandomBrush)cbBrushes.SelectedItem;
                }
                return null;
            }
        }
    }
}
